<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class User extends PC_controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('Usermodel','m_user');
		$this->load->model('Groupmodel','m_group');
	}

	public function index(){
		$data = parent::index();
		$user = $this->session->userdata('user');
		$data['attr']['group_list'] = $this->m_group->outputGroupList4js();
		$data['attr']['group_available_list'] = $user['group_level'] > 0 ? $this->m_group->outputAvailableGroupList4js() : '[]';
		$this->load->view('admin/user',$data);
	}

	public function getUserList(){
		$assist['where'] = getFilter($this->input->post(),Usermodel::$searchField);
		$assist['like'] = getFilter($this->input->post(),Usermodel::$likeField);
		$assist['sort'] = getSort($this->input->post(),Usermodel::$sortField);
		$assist['pagination'] = getPagination($this->input->post());

		list($list,$count) = $this->m_user->getUserListSection($assist);

		$this->response->setSuccess();
		$this->response->setItems($list);
		$this->response->setTotalCount($count);
		$this->response->responseExit();
	}

	public function addUser(){
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('user_code','string');
		$this->validate->setRule('user_name','string');
		$this->validate->setRule('user_phone','string');
		$this->validate->setRule('user_phone','notnull',false);
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		if(!$this->m_group->checkGroupLevelAvailable($info['group_id'])) {
			$this->response->setDetail('添加管理员权限超过本人权限!');
			$this->response->responseExit();
		}
		if(!$this->m_user->checkCodeAvailable($info['user_code'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}
		if(!$this->m_user->checkNameAvailable($info['user_name'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}

		$info['user_status'] = STATUS_ACTIVE;
		$info['user_password'] = md5(DEFAULT_PASSWORD.SALT);
		$info['user_time_create'] = NOW;
		if(empty($info['user_phone'])) {
			$info['user_phone'] = NULL;
		}
		if(!$this->m_user->addUser($info)) {
			$this->response->setDetail('手机号码已存在!');
			$this->response->responseExit();
		}
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function editUser(){
		$this->validate->setRule('user_id','integer');
		$this->validate->setRule('group_id','integer');
		$this->validate->setRule('user_code','string');
		$this->validate->setRule('user_name','string');
		$this->validate->setRule('user_phone','string');
		$this->validate->setRule('user_phone','notnull',false);
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();

		
		if(!$this->m_user->checkCodeAvailable($info['user_code'],$info['user_id'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}
		if(!$this->m_user->checkNameAvailable($info['user_name'],$info['user_id'])){
			$this->response->setDetail($this->lang->line('error_used'));
			$this->response->responseExit();
		}

		if(empty($info['user_phone'])) {
			$info['user_phone'] = NULL;
		}
		if(!$this->m_user->editUser($info)) {
			$this->response->setDetail('手机号码已存在!');
			$this->response->responseExit();
		}
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function resetPassword(){
		$this->validate->setRule('user_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_password'] = md5(DEFAULT_PASSWORD.SALT);
		
		$this->m_user->editUser($info);
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}

	public function disableUser(){
		$this->validate->setRule('user_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_status'] = STATUS_DISABLE;
		
		$this->m_user->editUser($info);
		

		$this->response->setSuccess();
		$this->response->responseExit();
	}

	public function activeUser(){
		$this->validate->setRule('user_id','integer');
		if(!$this->validate->run($this->input->post())){
			$this->response->setDetail($this->validate->getMessage());
			$this->response->responseExit();
		}
		$info = $this->validate->getResult();
		$info['user_status'] = STATUS_ACTIVE;
		
		$this->m_user->editUser($info);
		

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
/* End of file user.php */
/* Location: ./application/controllers/user.php */